//
//  StatusFrame.swift
//  SinaWeibo
//
//  Created by 郭平 on 15/10/16.
//  Copyright © 2015年 郭平. All rights reserved.
//

import UIKit

let CELL_CHILD_VIEW_MARGIN: CGFloat = 10    // 整体cell里面的间隔
let CELL_STATUS_NAME_FONT: CGFloat = 14     // 名称的字体大小
let CELL_STATUS_CREATE_AT_FONT: CGFloat = 14 // 发表时间/来源字体大小
let CELL_STATUS_TEXT_FONT: CGFloat = 14     // 原创微博内容字体大小

class StatusFrame: NSObject {
    
    var status: Status? {
        didSet{
        
            calcFrame()     // 计算cell里面的每个子控件
        }
    }
    
    var originalViewF: CGRect?      // 原创微博整体大小
    var cellHeight: CGFloat = 0     // cell的高度
    var headImageViewF: CGRect?     // 头像
    var nameLabelF: CGRect?         // 名称
    var vipImageViewF: CGRect?      // 会员图标
    var createdTimeLabelF: CGRect?  // 发表时间
    var sourceLabelF: CGRect?       // 来源
    var originalTextLabelF: CGRect? // 原创微博内容
    var originalPhotosF: CGRect?    // 原创微博配图
    
    var retweetViewF: CGRect?       // 转发微博View大小
    var retweetTextLabelF: CGRect?  // 转发微博内容大小
    var retweetPhotosF: CGRect?     // 转发微博图片大小
    
    var statusToolBarF: CGRect?     // 底部toolBar
    
    /**
        计算frame
    */
    private func calcFrame() {
        
        let status = self.status!
        
        let originalViewX: CGFloat = 0
        let originalViewY = CELL_CHILD_VIEW_MARGIN
        let originalViewW = SCREENW
        
        /// 头像
        let headImageViewX = CELL_CHILD_VIEW_MARGIN
        let headImageViewY = CELL_CHILD_VIEW_MARGIN
        let headImageViewSize = CGSizeMake(35, 35)
        
        headImageViewF = CGRect(origin: CGPoint(x: headImageViewX, y: headImageViewY), size: headImageViewSize)
        
        /// 名称
        let nameLabelX = CGRectGetMaxX(headImageViewF!) + CELL_CHILD_VIEW_MARGIN
        let nameLabelY = headImageViewY
        let nameLabelSize = status.user!.name!.size(UIFont.systemFontOfSize(CELL_STATUS_NAME_FONT))
        nameLabelF = CGRect(origin: CGPoint(x: nameLabelX, y: nameLabelY), size:nameLabelSize)
        
        /**
        *  会员
        */
        if status.user!.isVip {
            
            let vipImageViewX = CGRectGetMaxX(nameLabelF!) + CELL_CHILD_VIEW_MARGIN
            let vipImageViewY = headImageViewY
            let vipImageViewSize = CGSizeMake(nameLabelSize.height, nameLabelSize.height)
            vipImageViewF = CGRect(origin: CGPoint(x: vipImageViewX, y: vipImageViewY), size: vipImageViewSize)
        }
        
        /// 时间
        let createdTimeLabelX = nameLabelX
        let createdTimeLabelSize = status.createdAtStr.size(UIFont.systemFontOfSize(CELL_STATUS_CREATE_AT_FONT))
        let createdTimeLabelY = CGRectGetMaxY(headImageViewF!) - createdTimeLabelSize.height
        createdTimeLabelF = CGRect(origin: CGPoint(x: createdTimeLabelX, y: createdTimeLabelY), size:createdTimeLabelSize)
    
        /// 来源
        let sourceLaelX = CGRectGetMaxX(createdTimeLabelF!) + CELL_CHILD_VIEW_MARGIN
        let sourceLabelY = createdTimeLabelY
        let sourceLabelSize = status.source?.size(UIFont.systemFontOfSize(CELL_STATUS_CREATE_AT_FONT))
        sourceLabelF = CGRect(origin: CGPoint(x: sourceLaelX, y: sourceLabelY), size: sourceLabelSize!)
        
        // 原创微博内容
        let originalTextLabelX = CELL_CHILD_VIEW_MARGIN
        let originalTextLabelY = CGRectGetMaxY(headImageViewF!) + CELL_CHILD_VIEW_MARGIN
        let originalTextLabelSize = status.text?.size(UIFont.systemFontOfSize(CELL_STATUS_TEXT_FONT), constrainedToSize: CGSizeMake(SCREENW - CELL_CHILD_VIEW_MARGIN * 2, CGFloat(MAXFLOAT)))
        originalTextLabelF = CGRect(origin: CGPoint(x: originalTextLabelX, y: originalTextLabelY), size: originalTextLabelSize!)
        
        var originalViewH = CGRectGetMaxY(originalTextLabelF!) + CELL_CHILD_VIEW_MARGIN
        
        // 判断是否有配图,及设置大小
        
        if let originalPicUrls = status.pic_urls where originalPicUrls.count>0 {
            let originalPhotosX = CELL_CHILD_VIEW_MARGIN
            let originalPhotosY = CGRectGetMaxY(originalTextLabelF!) + CELL_CHILD_VIEW_MARGIN
            let originalPhotosSize = StatusPhotos.size(originalPicUrls.count)
            originalPhotosF = CGRect(origin: CGPoint(x: originalPhotosX, y: originalPhotosY), size: originalPhotosSize)
            
             originalViewH = CGRectGetMaxY(originalPhotosF!) + CELL_CHILD_VIEW_MARGIN
        }
        
        // 设置原创微博整体View
        originalViewF = CGRectMake(originalViewX, originalViewY, originalViewW, originalViewH)
        
        var statusToolBarY = CGRectGetMaxY(originalViewF!)
        
        //MARK: - 是否有转发微博
        if let retStatus = status.retweeted_status {
            
            let retweetViewX: CGFloat = 0
            let retweetViewY = CGRectGetMaxY(originalViewF!)
            let retweetViewW = SCREENW
            
            // 转发微博内容大小
            let retweetTextLabelX = CELL_CHILD_VIEW_MARGIN
            let retweetTextLabelY = CELL_CHILD_VIEW_MARGIN
            let retweetTextLabelSize = retStatus.text?.size(UIFont.systemFontOfSize(CELL_STATUS_TEXT_FONT), constrainedToSize: CGSizeMake(SCREENW - CELL_CHILD_VIEW_MARGIN * 2, CGFloat(MAXFLOAT)))
            
            retweetTextLabelF = CGRect(origin: CGPointMake(retweetTextLabelX, retweetTextLabelY), size: retweetTextLabelSize!)
            
            var retweetViewH = CGRectGetMaxY(retweetTextLabelF!) + CELL_CHILD_VIEW_MARGIN
            
            // 判断是否有转发微博配图
            if let retweetPicUrl = retStatus.pic_urls where retweetPicUrl.count>0 {
                
                let retweetPhotosX = CELL_CHILD_VIEW_MARGIN
                let retweetPhotosY = CGRectGetMaxY(retweetTextLabelF!) + CELL_CHILD_VIEW_MARGIN
                let retweetPhotosSize = StatusPhotos.size(retweetPicUrl.count)
                retweetPhotosF = CGRect(origin: CGPointMake(retweetPhotosX, retweetPhotosY), size: retweetPhotosSize)
                retweetViewH = CGRectGetMaxY(retweetPhotosF!) + CELL_CHILD_VIEW_MARGIN
            }
            //转发微博整体的View的高度
            retweetViewF = CGRect(origin: CGPointMake(retweetViewX, retweetViewY), size: CGSizeMake(retweetViewW, retweetViewH))
            
            //重新赋值
            statusToolBarY = CGRectGetMaxY(retweetViewF!)
        }
        
        //MARK: - 底部toolBar
        let statusToolBarX: CGFloat = 0
        let statusToolBarSize = CGSizeMake(SCREENW, 35)
        statusToolBarF = CGRect(origin: CGPoint(x: statusToolBarX, y: statusToolBarY), size: statusToolBarSize)
        
        cellHeight = CGRectGetMaxY(statusToolBarF!)
    }
}
